


var state = "question";

jQuery(document).ready(function() {
		
	
	

	
	//$("#show_iframe").confind("*").not('body').not('html').not('textarea').mouseup( 
  $("#contentdiv1").not('body').not('html').not('textarea').mouseup( 
		function() { 
			var sel = getSelectedText();
            
            if (state == "question") {
			  $('#card_front').val( sel );
              state = "answer";
            }
            else if (state == "answer") {
              $('#card_back').val( sel );
              state = "done";
            }
		}
	);


  $(".card_icon").click( 
    function() { 
      $('#card_front').val( $(this).find('.front').html() );
      $('#card_back').val( $(this).find('.back').html() );
    }
  );	
	
});


function getSelectedText(){
	var txt = '';
	if (window.getSelection){
		txt = window.getSelection();
	} else if (document.getSelection){
		txt = document.getSelection();
	} else if (document.selection){
		txt = document.selection.createRange().text;
	} else { return;}
    var range = txt.getRangeAt(0);
    var newNode = document.createElement("span");
    if (state == "question") {
      newNode.setAttribute("style", "background-color: yellow;");
      document.getElementById("instructions").innerHTML = "Select answer text";
    }
    else if (state == "answer") {
      newNode.setAttribute("style", "background-color: purple;");
    }
    range.surroundContents(newNode);

	return txt;
}